import Vue from 'vue'
import App from './App.vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import './assets/global.css';
import axios from "axios";
import VueRouter from 'vue-router';
import router from './router';
import store from './store';

Vue.prototype.$axios = axios;
Vue.prototype.$httpUrl = 'http://106.15.93.207:9090/api'; 
Vue.config.productionTip = false;

Vue.use(ElementUI, { size: 'small' } );
Vue.use(VueRouter);

// 注入请求拦截器：自动附加 Authorization
axios.interceptors.request.use(
  (config) => {
    const token = sessionStorage.getItem('jwtToken');
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  },
  (error) => Promise.reject(error)
);

// 响应拦截器：统一处理 401 未授权
axios.interceptors.response.use(
  (response) => response,
  (error) => {
    if (error && error.response && error.response.status === 401) {
      sessionStorage.clear();
      router.push('/');
    }
    return Promise.reject(error);
  }
);

// 创建 Vue 根实例
new Vue({
    router,
    store,
    render: h => h(App),
}).$mount('#app')
